<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.SubstoreDAO" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.io.File"%>
<%@page import="java.util.*"%>
<%@page import="javazoom.upload.*"%> 
<%@page import="uploadutilities.FileMover"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="jxl.Cell"%>
<%@page import="jxl.Sheet"%>
<%@page import="jxl.Workbook"%>
<%@page import="jxl.WorkbookSettings"%>
<%@page import="db.FororderDAO"%>
<%@page import="db.Fororder"%>
<%@page import="db.Forderpurmat"%>
<%@page import="db.Fororder"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Material"%>
<%@page import="db.ForderpurmatDAO"%>
<%@page import="db.Receiveorder"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.Group" %>
<%@page import="db.Receivepurmat"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.Place"%>
<%@page import="db.PlaceDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%@ include file="/jsp/common/utility/ImportExecl.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	FororderDAO fororderDAO = (FororderDAO)ctx.getBean("FororderDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	PlaceDAO placeDAO = (PlaceDAO)ctx.getBean("PlaceDAO");
	ReceiveorderDAO receiveorderDAO = (ReceiveorderDAO)ctx.getBean("ReceiveorderDAO");
	ReceivepurmatDAO receivepurmatDAO = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	ForderpurmatDAO forderpurmatDAO = (ForderpurmatDAO)ctx.getBean("ForderpurmatDAO");
	int uid = 0 ;
	if(session.getAttribute("id")!=null){
		uid = (Integer)session.getAttribute("id");
	}
	FileMover fileMover = new FileMover();//你也可以使用自带的实例中jsp:useBean的形式。
	UploadBean upBean = new UploadBean();
	MultipartFormDataRequest mrequest = null;
	int tid = (Integer)session.getAttribute("tid");
	Hashtable files = null;
	boolean success = true;
	String message = "恭喜您，批量入库成功！";
	String serverPath = new String();
	serverPath = request.getParameter("serverPath");
	List listjxl = new ArrayList();
	if(serverPath!=null){
		listjxl = getListByJxl(serverPath,success,message);
		request.setAttribute("serverPath",serverPath);
	}
	List listmap = new ArrayList();
	
	if(listjxl!=null && listjxl.size()>0){
		for(int i=0;i<listjxl.size();i++){
			List li = (List)listjxl.get(i);
			
			
			String pr = "";
			if(li.get(5)!=null && (li.get(5)!="" || !"".equals(li.get(5)))){
					pr = (String)li.get(5);
					if(pr.lastIndexOf("|")!=-1){//找到结合货位例如  A001专项|Mjb10ca00200|   则进行字符截取。只取中间的项目编码。
						String[] strs = pr.split("\\|");
						pr = strs[1];
					}
					//System.out.println("pr===========>>>>>"+pr);
			}
			if(li!=null && li.size()>=11){
				//建立虚拟订单接收过程。主要为保存数据统一，供应商存入订单接收表中
				Receiveorder receiveorder = new Receiveorder();
				receiveorder.setCode("");//采购单接收号，由于execl导入没有接收号。
				receiveorder.setDate(new Date());
				receiveorder.setIsDelete(0);
				receiveorder.setProCode(pr);//项目编码
				
				SimpleDateFormat codesdf = new SimpleDateFormat("yyyyMMddHHmmss");
				receiveorder.setRcode("R" + codesdf.format(new Date()));//execl导入入库单号
				User user = userDAO.findById(uid);
				Group group = user.getGroup();
				if(group.getName()!=null){
					receiveorder.setReceiveCompany(group.getName());//接收人单位
				}else{
					receiveorder.setReceiveCompany("");//接收人单位
				}
				receiveorder.setReceiveCompany(group.getName());//接收人单位
				receiveorder.setReceiverDate(new Date());
				receiveorder.setReceiverUserId(uid);
				receiveorder.setRemark("execl");
				if(li.get(4)!=null && !"".equals(li.get(4))){
					String zk = (String)li.get(4);
					if(zk.indexOf("通用")!=-1){
						receiveorder.setSubStore(2);//接收子库id，通用
					}else if(zk.indexOf("专项")!=-1){
						receiveorder.setSubStore(1);//接收子库id，专项
					}else{
						receiveorder.setSubStore(2);//接收子库id，默认通用
					}
				}else{
					receiveorder.setSubStore(2);//接收子库id，默认通用
				}
				
				if(li.get(1)!=null){
					receiveorder.setSupplier((String)li.get(1));//供应商
				}else{
					receiveorder.setSupplier("");//供应商
				}
				receiveorder.setTeamId(tid);
				receiveorder.setTmpCode("");//采购订单号
				receiveorder.setTopType("储备");//默认储备
				receiveorderDAO.save(receiveorder);
				receiveorder.getId();
				//更新
				receiveorder.setRcode("R" + receiveorder.getId());//execl导入入库单号
				receiveorderDAO.attachDirty(receiveorder);
				
				/////建立虚拟订单接收成功进行物资填充
				Receivepurmat receivepurmat =new Receivepurmat();
				
				//List matlist = materialDAO.findByCode(li.get(2));
				List matlist = materialDAO.findCodeAndTeamId(li.get(2),tid);
				//System.out.println("##########"+li.get(2));
				if(matlist!=null && matlist.size()>0){
					for(int m=0;m<matlist.size();m++){
						Material material = (Material)matlist.get(m);
						if(material.getIsDelete()==0){//找到物资
							receivepurmat.setMaterialId(material.getId());//物资id
							
						}else{
							//系统自动建立
							Material newmaterial = new Material();
							newmaterial.setCode((String)li.get(2));
							newmaterial.setCompany((String)li.get(7));
							newmaterial.setIsDelete(0);
							newmaterial.setDate(new Date());
							newmaterial.setName((String)li.get(3));
							newmaterial.setTeamId(tid);
							materialDAO.save(newmaterial);
							receivepurmat.setMaterialId(newmaterial.getId());//物资id
						}
					}
					
				}else{
					//系统自动建立
							Material newmaterial = new Material();
							newmaterial.setCode((String)li.get(2));
							newmaterial.setCompany((String)li.get(7));
							newmaterial.setIsDelete(0);
							newmaterial.setDate(new Date());
							newmaterial.setName((String)li.get(3));
							newmaterial.setTeamId(tid);
							materialDAO.save(newmaterial);
							receivepurmat.setMaterialId(newmaterial.getId());//物资id
				}
				if(li.get(6)!=null){//批次
					receivepurmat.setBatch((String)li.get(6));
				}else{
					receivepurmat.setBatch("");
				}
				receivepurmat.setDate(new Date());
				if(li.get(10)!=null){
					receivepurmat.setImportNum(Integer.parseInt((String)li.get(10)));//最终入库数量即库存数量
					receivepurmat.setStoreNum(Integer.parseInt((String)li.get(10)));//新增最终入库数量，该数量不可变跟出库不受影响
				}else{
					receivepurmat.setImportNum(0);//最终入库数量即库存数量
					receivepurmat.setStoreNum(0);//新增最终入库数量，该数量不可变跟出库不受影响
				}
				receivepurmat.setIsDelete(0);
				if(li.get(0)!=null && !"".equals(li.get(0))){
					
					List plalist = placeDAO.findByName(li.get(0));
					if(plalist!=null && plalist.size()>0){
						//库位存在
						Place place =(Place)plalist.get(0);
						receivepurmat.setPlaceId(place.getId());//库位id
					}else{
						//不存在
						Place place1 = new Place();
						place1.setName((String)li.get(0));
						placeDAO.save(place1);
						receivepurmat.setPlaceId(place1.getId());//库位id
					}
				}else{
					receivepurmat.setPlaceId(0);//库位id
				}
				
				receivepurmat.setProCode(pr);//项目编码
				
				
				
				if(li.get(10)!=null){
					receivepurmat.setPurNum(Integer.parseInt((String)li.get(10)));//物资实际数量。execl默认为最终导入数量
				}else{
					receivepurmat.setPurNum(0);//物资实际数量。execl默认为最终导入数量
				}
				if(li.get(8)!=null){
					receivepurmat.setPurPrice(Float.parseFloat(((String)li.get(8)).replace(",","")));//价格
				}else{
					receivepurmat.setPurPrice(Float.parseFloat("0"));//价格
				}
				if(receiveorder.getId()!=null){
					receivepurmat.setReceiveOrderId(receiveorder.getId());
				}else{
					receivepurmat.setReceiveOrderId(0);
				}
				if(li.get(4)!=null && !"".equals(li.get(4))){
					String zk = (String)li.get(4);
					if(zk.indexOf("通用")!=-1){
						receivepurmat.setSubStore(2);//接收子库id，通用
					}else if(zk.indexOf("专项")!=-1){
						receivepurmat.setSubStore(1);//接收子库id，专项
					}else{
						receivepurmat.setSubStore(2);//接收子库id，默认通用
					}
				}else{
					receivepurmat.setSubStore(2);//接收子库id，默认通用
				}
				receivepurmat.setTeamId(tid);
				
				if(li.get(10)!=null){
					receivepurmat.setUseNum(Integer.parseInt((String)li.get(10)));//可用数量，于库存一致
				}else{
					receivepurmat.setUseNum(0);//可用数量，于库存一致。execl默认为最终导入数量
				}
				receivepurmat.setUserId(uid);
				receivepurmatDAO.save(receivepurmat);
				
				
			}else{
				success = false;
				message = "解析出错，execl文件格式行数不够，请仔细检查execl文件。";
				request.setAttribute("success",success);
				request.setAttribute("message",message);
				pageContext.forward("/vm/stock/stockImportExecl.vm");
				return;
			}
			
			
		}
	}
	request.setAttribute("listjxl",listmap);
	request.setAttribute("success",success);
	request.setAttribute("message",message);
	pageContext.forward("/vm/stock/stockImportExecl.vm");
%>